package com.hancher.gamelife2.diary;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.hancher.common.androidutil.PathUtil;
import com.hancher.common.javautil.EmptyUtil;
import com.hancher.common.rv.BaseVBAdapter;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.ItemUploadPictureBinding;

import java.io.File;

import lombok.Data;

public class DiaryEditAdapter extends BaseVBAdapter<ItemUploadPictureBinding, DiaryEditAdapter.ImageItem> {

    @Override
    protected ItemUploadPictureBinding createViewBinding(LayoutInflater from, ViewGroup viewGroup) {
        return ItemUploadPictureBinding.inflate(from, viewGroup, false);
    }

    @Override
    protected void convertItem(ItemUploadPictureBinding binding, int position, ImageItem item) {
        if (item.getType() == -1) {
            Glide.with(binding.imgUpload).load(R.drawable.upload_photo).into(binding.imgUpload);
        } else if (EmptyUtil.isNotEmpty(item.getPath()) && new File(item.getPath()).exists()) {
            Glide.with(binding.imgUpload).load(item.getPath()).into(binding.imgUpload);
        } else if (EmptyUtil.isNotEmpty(item.getBaiduPath()) && new File(PathUtil.OUT_APP_CACHE + item.getBaiduPath()).exists()) {
            Glide.with(binding.imgUpload).load(PathUtil.OUT_APP_CACHE + item.getBaiduPath()).into(binding.imgUpload);
        } else if (EmptyUtil.isNotEmpty(item.getBaiduThumbUrl())) {
            Glide.with(binding.imgUpload).load(item.getBaiduThumbUrl()).into(binding.imgUpload);
        } else {
            Glide.with(binding.imgUpload).load(R.drawable.icon_image_loss).into(binding.imgUpload);
        }
        binding.imgRemove.setVisibility(item.getType() != -1 ? View.VISIBLE : View.GONE);
    }

    @Data
    public static class ImageItem {
        private String path;
        private String baiduPath;
        private String baiduFsId;
        private String baiduThumbUrl;
        // 0未上传，1已上传，-1添加
        private int type = -1;
    }
}
